//
//  RHFaultPushConfigVC.m
//  RuiHeApp
//
//  Created by Yan Chen on 2025/2/13.
//

#import "RHFaultPushConfigVC.h"
#import "ZYSwitch.h"
@interface RHFaultPushConfigVC ()
@property (nonatomic ,strong) ZYSwitch *faultSwitch;

@end

@implementation RHFaultPushConfigVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = CFTabViewBgColor;
    self.navigationItem.title = @"设置".rh_localized;
    [self setupUI];
    [self getPushStatus];
}

-(void)setupUI
{
    kWeakSelf;
    UIView *bgView = kView;
    bgView.backgroundColor = UIColor.whiteColor;
    [bgView rounded:8];
    [self.view addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_offset(15);
        make.right.mas_offset(-15);
        make.height.offset(50);
    }];
    
    _faultSwitch = [[ZYSwitch alloc]initWithFrame:CGRectMake(0, 0, 45, 25)];
    [[_faultSwitch rac_signalForControlEvents:UIControlEventValueChanged]subscribeNext:^(__kindof UIControl * _Nullable x) {
            [weakSelf faultPushConfig];
    }];
    [bgView addSubview:_faultSwitch];
    [_faultSwitch mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(@(CGSizeMake(45, 25)));
        make.right.mas_offset(-15);
        make.centerY.mas_offset(0);
    }];
    
    QMUILabel *titleLab = [QMUILabel labelWithText:@"故障邮件推送".rh_localized font:kSYSTEMFONT(14) textColor:kCOLOR_HEX(@"#171A1D")];
    titleLab.textAlignment = NSTextAlignmentLeft;
    [bgView addSubview:titleLab];
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.equalTo(weakSelf.faultSwitch.mas_left).offset(-10);
        make.centerY.mas_offset(0);
    }];
    
    
}

-(void)faultPushConfig
{
    NSDictionary *para = @{@"isPush":@(self.faultSwitch.isOn),
                           @"userId":@([PrivateMange share].currentUser.userId)};
    [QMUITips showLoadingInView:kAppWindow];
    [BaseModel startGetWithCompletionAPIKey:H_deviceFaultPushConfig argument:para requestSuccess:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        if (resultModel.code == 200) {
            [QMUITips showSucceed:@"成功".rh_localized];
        }
    } requestFail:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        
    }];
}

-(void)getPushStatus
{
    [QMUITips showLoadingInView:kAppWindow];
    [BaseModel startGetWithCompletionAPIKey:H_getDeviceFaultPushConfig argument:@{} requestSuccess:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        if (resultModel.code == 200) {
            self.faultSwitch.on = [request.responseJSONObject[@"data"]integerValue];
        }else{
            self.faultSwitch.on = !self.faultSwitch.isOn;
        }
    } requestFail:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        self.faultSwitch.on = !self.faultSwitch.isOn;
    }];
}
@end
